package com.sophie.util;

import com.alibaba.fastjson2.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: Rowena
 * @CreateTime: 2024-09-04 09:19:43
 * @Version: 1.0
 */

public class SendJSONUtil {
    public static <T> void send(T obj, HttpServletResponse response){
        try {
            //1.设置传递给前端的数据的类型
            response.setContentType("application/json;charset=utf-8");
            //2.设置响应编码格式
            response.setCharacterEncoding("utf-8");
            //3.获取字符输出流
            PrintWriter pw = response.getWriter();
            //4.将对象转换成json格式的字符串
            String jsonString= JSON.toJSONString(obj);
            pw.println(jsonString);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
